package com.ljfiction.book.http;

import android.content.Context;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串操作工具包<br>
 * <p/>
 * <b>创建时间</b> 2014-8-14
 *
 * @author kymjs (https://github.com/kymjs)
 * @version 1.1
 */
public class StringUtils {



    public static String creatAcacheKey(Object... param) {
        String key = "";
        for (Object o : param) {
            key += "-" + o;
        }
        return key.replaceFirst("-","");
    }
    private final static Pattern emailer = Pattern
            .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    private final static Pattern phone = Pattern
            .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

    //^((?=.*[0-9])(?=.*[a-zA-Z!@#$%^&*()_+|{}?><\-\]\\[\/]).{6,16})|((?=.*[a-zA-Z])(?=.*[0-9!@#$%^&*()_+|{}?><\-\]\\[\/]).{6,16})|((?=.*[0-9a-zA-Z])(?=.*[!@#$%^&*()_+|{}?><\-\]\\[\/]).{6,16})$
    // 判断登录密码格式是否合法
    public static boolean isMiMa(String mima) {
        Pattern p = Pattern
                .compile("^((?=.*[0-9])(?=.*[a-zA-Z\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\|\\{\\}\\?\\>\\<\\-\\]\\\\\\[\\/\\]]).{6,16})|((?=.*[a-zA-Z])(?=.*[0-9\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\|\\{\\}\\?\\>\\<\\-\\]\\\\\\[\\/\\]]).{6,16})|((?=.*[0-9a-zA-Z])(?=.*[\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\|\\{\\}\\?\\>\\<\\-\\]\\\\\\[\\/\\]]).{6,20})$");
        Matcher m = p.matcher(mima);
        return m.matches();
    }

    /**
     * 判断给定字符串是否空白串 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     */
    public static boolean isEmpty(CharSequence input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    //显示金额
    public static String showMoney(int money) {
        DecimalFormat decimalFormat = new DecimalFormat("###,###");
        return decimalFormat.format(money);
    }

    //显示金额小数点两位
    public static String showMoney(double money) {
        DecimalFormat decimalFormat = new DecimalFormat("###,##0.00");
        return decimalFormat.format(money);
    }

    //显示金额小数点两位
    public static String showMoney2(double money) {
        DecimalFormat decimalFormat = new DecimalFormat("######.##");
        return decimalFormat.format(money);
    } //显示金额小数点两位

    public static String showMoney2(String money) {
        try {
            if (isDouble(money)) {
                DecimalFormat decimalFormat = new DecimalFormat("###,##0.00");
                return decimalFormat.format(Double.parseDouble(money));
            } else {
                return money;
            }
        } catch (Exception e) {
            return money;
        }
    }

    //显示金额小数点两位
    public static String showMoney(String money) {

        try {
            if (isDouble(money)) {
                DecimalFormat decimalFormat = new DecimalFormat("###,##0.00");
                return decimalFormat.format(Double.parseDouble(money));
            } else {
                return money;
            }
        } catch (Exception e) {
            return money;
        }
    }

    //显示金额小数点两位
    public static String showMoney3(String money) {

        try {
            if (isDouble(money)) {
                DecimalFormat decimalFormat = new DecimalFormat("###,###.##");
                return decimalFormat.format(Double.parseDouble(money));
            } else {
                return money;
            }
        } catch (Exception e) {
            return money;
        }
    }

    //将两个金额相减后小数点两位
    private static Double d_all;
    private static Double d_now;

    public static String transtion(String number_all, String number_already) {
        try {
            if (!StringUtils.isEmpty(number_all)) {
                d_all = Double.valueOf(number_all);
            } else {
                d_all = Double.valueOf("0.0");
            }
            if (!StringUtils.isEmpty(number_already)) {
                d_now = Double.valueOf(number_already);
            } else {
                d_now = Double.valueOf("0.0");
            }
            Double d_surplus = d_all - d_now;
            //转化后的剩余金额
            BigDecimal num_already = new BigDecimal(d_surplus.toString());
            String already_money = StringUtils.showMoney2(num_already.toString());
            return already_money;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 判断字符是否可以转换成double类型
     */
    public static boolean isDouble(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException ex) {
        }
        return false;
    }

    /**
     * 判断给定字符串是否空白串 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     */
    public static boolean isEmpty(CharSequence... strs) {
        for (CharSequence str : strs) {
            if (isEmpty(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是不是一个合法的电子邮件地址
     */
    public static boolean isEmail(CharSequence email) {
        if (isEmpty(email))
            return false;
        return emailer.matcher(email).matches();
    }

    /**
     * 判断是不是一个合法的手机号码
     */
    public static boolean isPhone(CharSequence phoneNum) {
        if (isEmpty(phoneNum))
            return false;
        return phone.matcher(phoneNum).matches();
    }

    public static boolean isSpace(String s) {
        return (s == null || s.trim().length() == 0);
    }

    /**
     * 返回当前系统时间
     */
    public static String getDataTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 字符串转整数
     *
     * @param str
     * @param defValue
     * @return
     */
    public static int toInt(String str, int defValue) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
        }
        return defValue;
    }

    /**
     * 对象转整
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static int toInt(Object obj) {
        if (obj == null)
            return 0;
        return toInt(obj.toString(), 0);
    }

    /**
     * String转long
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static long toLong(String obj) {
        try {
            return Long.parseLong(obj);
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * String转double
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static double toDouble(String obj) {
        try {
            return Double.parseDouble(obj);
        } catch (Exception e) {
        }
        return 0D;
    }

    /**
     * 字符串转布尔
     *
     * @param b
     * @return 转换异常返回 false
     */
    public static boolean toBool(String b) {
        try {
            return Boolean.parseBoolean(b);
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * 判断一个字符串是不是数字
     */
    public static boolean isNumber(CharSequence str) {
        try {
            Integer.parseInt(str.toString());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * byte[]数组转换为16进制的字符串。
     *
     * @param data 要转换的字节数组。
     * @return 转换后的结果。
     */
    public static final String byteArrayToHexString(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length * 2);
        for (byte b : data) {
            int v = b & 0xff;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString().toUpperCase(Locale.getDefault());
    }

    /**
     * 16进制表示的字符串转换为字节数组。
     *
     * @param s 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] d = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个进制字节
            d[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return d;
    }

    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    /**
     * 以友好的方式显示时间
     *
     * @param sdate
     * @return
     */
    public static String friendlyTime(String sdate) {
        Date time = null;

        if (isInEasternEightZones()) {
            time = toDate(sdate);
        } else {
            time = transformTime(toDate(sdate), TimeZone.getTimeZone("GMT+08"),
                    TimeZone.getDefault());
        }

        if (time == null) {
            return "Unknown";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();

        // 判断是否是同一天
        String curDate = dateFormater2.get().format(cal.getTime());
        String paramDate = dateFormater2.get().format(time);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
            return ftime;
        }

        long lt = time.getTime() / 86400000;
        long ct = cal.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天 ";
        } else if (days > 2 && days < 31) {
            ftime = days + "天前";
        } else if (days >= 31 && days <= 2 * 31) {
            ftime = "一个月前";
        } else if (days > 2 * 31 && days <= 3 * 31) {
            ftime = "2个月前";
        } else if (days > 3 * 31 && days <= 4 * 31) {
            ftime = "3个月前";
        } else {
            ftime = dateFormater2.get().format(time);
        }
        return ftime;
    }

    /**
     * 将字符串转位日期类型
     *
     * @param sdate
     * @return
     */
    public static Date toDate(String sdate) {
        return toDate(sdate, dateFormater.get());
    }

    public static Date toDate(String sdate, SimpleDateFormat dateFormater) {
        try {
            return dateFormater.parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 判断用户的设备时区是否为东八区（中国） 2014年7月31日
     *
     * @return
     */
    public static boolean isInEasternEightZones() {
        boolean defaultVaule = true;
        if (TimeZone.getDefault() == TimeZone.getTimeZone("GMT+08"))
            defaultVaule = true;
        else
            defaultVaule = false;
        return defaultVaule;
    }

    /**
     * 根据不同时区，转换时间 2014年7月31日
     */
    public static Date transformTime(Date date, TimeZone oldZone,
                                     TimeZone newZone) {
        Date finalDate = null;
        if (date != null) {
            int timeOffset = oldZone.getOffset(date.getTime())
                    - newZone.getOffset(date.getTime());
            finalDate = new Date(date.getTime() - timeOffset);
        }
        return finalDate;
    }

    /**
     * 验证身份证号是否符合规则
     *
     * @param text 身份证号
     * @return
     */
    public static boolean personIdValidation(String text) {
        String regX = "[0-9]{17}X";
        String regx = "[0-9]{17}x";
        String reg1 = "[0-9]{15}";
        String regex = "[0-9]{18}";
        return text.matches(regx) || text.matches(reg1) || text.matches(regex) || text.matches(regX);
    }

    // 判断手机号码是否合法
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern
                .compile("^1\\d{10}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 显示登录用户的账号
     * 1.是手机号显示139****1259的样式
     * 2.是用户名的话显示用户名
     */
    public static String showUserName(String str) {
        if (isMobileNO(str)) {
            //是手机号
            StringBuilder sb = new StringBuilder(str);
            sb.replace(3, 7, "****");
            return sb.toString();
        } else {
            //非手机号
            return str;
        }
    }

    /**
     * 密文显示身份证号
     * 如4101*********0236
     *
     * @param str
     * @return
     */
    public static String showIdCard(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        StringBuilder sb = new StringBuilder(str);
        int amountNum = str.length();
        sb.replace(3, amountNum - 4, "***********");
        return sb.toString();
    }

    //判断验证码是否合法
    public static boolean isYZM(Context ctx, String yzm) {
        boolean isTure;
        if (yzm.length() == 0) {
            //MyToast.showMyToast(ctx, "验证码不能为空", 0);
            isTure = false;
        } else if (yzm.length() < 5) {
            //MyToast.showMyToast(ctx, "验证码格式错误", 0);
            isTure = false;
        } else {
            isTure = true;
        }
        return isTure;
    }

    /**
     * 显示用户的身份证号码
     *
     * @param str 用户的身份证号码
     * @return abc***********defg
     */
    public static String showAuthenticationBankIDNumber(String str) {
        StringBuilder sb = new StringBuilder(str);
        sb.replace(4, str.length() - 4, "***********");
        return sb.toString();
    }
    /**
     * @param str       字符串只能为两位小数或者整数
     * @param isDecimal 是否是小数
     * @Description 格式化字符串，每三位用逗号隔开
     */
    public static String addComma(String str, boolean isDecimal) {
        //先将字符串颠倒顺序
        str = new StringBuilder(str).reverse().toString();
        if (str.equals("0")) {
            return str;
        }
        String str2 = "";
        for (int i = 0; i < str.length(); i++) {
            if (i * 3 + 3 > str.length()) {
                str2 += str.substring(i * 3, str.length());
                break;
            }
            str2 += str.substring(i * 3, i * 3 + 3) + ",";
        }
        if (str2.endsWith(",")) {
            str2 = str2.substring(0, str2.length() - 1);
        }
        //最后再将顺序反转过来
        String temp = new StringBuilder(str2).reverse().toString();
        if (isDecimal) {
            //去掉最后的","
            return temp.substring(0, temp.lastIndexOf(",")) + temp.substring(temp.lastIndexOf(",") + 1, temp.length());
        } else {
            return temp;
        }
    }
}
